package com.hnjmxy.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import pojo.Customer;
import service.CustomerService;
import util.DataResponse;


/**
 * Servlet implementation class CustomerServlet
 */
@WebServlet("/CustomerServlet")
public class CustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//如果doget和dopost方法存在，service方法不再默认使用
		String method = request.getParameter("method");
		if(method.equals("login")) {
			login(request,response); //自定义的登录方法
		}else if(method.equals("reg")) {
			reg(request,response);  //自定义的注册方法
		}
	}
	
	

	public void reg(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String name=request.getParameter("c_name");
		String pwd=request.getParameter("c_pass");
		Customer c = new Customer();
		c.setC_name(name);
		c.setC_pass(pwd);
		CustomerService cservice =new  CustomerService();
		int flag = cservice.addCustomer(c);
		if(flag>0) {

			HttpSession session = request.getSession(true);
			session.setAttribute("c",flag);
			//跳转
		}
	}
	
		
	
		// TODO Auto-generated method stub
		
	

	public void login(HttpServletRequest request, HttpServletResponse response) {
		/**
		 * 校验验证码
		 * 1、从session中获取正确的验证码
		 * 2、从表单中获取用户填写的验证码
		 * 3、进行比较
		 * 4、如果相同向下运行，否则保存错误信息到request域，转发到login.jsp
		 */
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String name=request.getParameter("c_name");
		String pwd=request.getParameter("c_pass");
		Customer c = new Customer();
		c.setC_name(name);
		c.setC_pass(pwd);
		//调用业务逻辑
		CustomerService cservice =new  CustomerService();
		Customer cu = cservice.loginByName(c);
		//响应给用户
		if(cu!=null) {
			DataResponse d=new DataResponse("200","success-xia",cu);//封装返回给前端的数据
			//把d转换成json字符串---》导入Gson.jar   产生json字符串的工具
			String json=new Gson().toJson(d);
			System.out.println(json);
			try {
				response.getWriter().write(json);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}else {
		DataResponse d=new DataResponse("500","error-xia",cu);//封装返回给前端的数据
		String json=new Gson().toJson(d);
		System.out.println(json);
		try {
			response.getWriter().write(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	}
}
